<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;

/**
* 订单管理
*/
class Weborder extends AdminBase
{
	
	protected function _initialize()
	{
		parent::_initialize();
        //获取列表导航信息,2为单页
        $class_type = db('nav')->where(['status'=>1])->order(['sort' => 'DESC', 'id' => 'ASC'])->column('name','id');
        $this->assign('class_type', $class_type);
	}
	public function index(){
		$nid = input('nid');	$userName = input('userName');  $tel = input('tel');
		$nid?$where['n.nid']=$nid:'';
        $userName?$where['o.userName']=['like',"%{$userName}%"]:'';
        $tel?$where['o.tel']=['like',"%{$tel}%"]:'';

		$list = db('order')->alias('o')
                ->join('__NEWS__ n','o.nid=n.id')
                ->join('__NAV__ a','a.id=n.nid')
                ->field('o.id,o.userName,o.tel,o.age,o.num,o.price,o.status,o.add_time,a.name')
                ->where($where)->order('add_time desc')->paginate(15);
		return $this->fetch('index',['list'=>$list]);
	}

	public function edit($id){
        $rows = db('order')->alias('o')
            ->join('__NEWS__ n','o.nid=n.id')
            //->join('__NAV__ a','a.id=n.nid')
            ->field('o.*,n.title,n.address,n.startTime,n.endTime')
            ->find($id);
		return $this->fetch('edit',['rows'=>$rows]);
	}
	public function update(){
 		if($this->request->isPost()) {
            $data = $this->request->except('content', 'post');
            $data['content'] = $_POST['content'];
            if (db('news')->update($data) !== false) {
                $this->success('更新成功', 'index');
            } else {
                $this->error('更新失败');
            }
        }
	}
	/*
	 * 更新'0'或'1'
	 */
	public function flag($id){
        if (request()->isPost()) {
            $data = input('post.');
            if (db('news')->where(['id' => $id])->update($data) !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }
    }
    public function delete($id = 0, $ids = [])
    {
        $id = $ids ? $ids : $id;
        if ($id) {
            if (db('news')->delete($id)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('请选择需要删除的项目');
        }
    }
}